package cn.wen233.srpc.core.loadbalance;

import cn.wen233.srpc.basic.loadbalance.LoadBalance;
import cn.wen233.srpc.basic.model.ServerInstance;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;

/**
 * 轮询式
 *
 * @author wenei
 * @date 2021-12-26 18:55
 */
@Slf4j
public class RoundLoadBalance implements LoadBalance {

    private Integer index = 0;

    @Override
    public ServerInstance select(List<ServerInstance> instances) {
        Integer length = instances.size();
        log.info("轮询式 loadBalance，instances={}", instances);
        if (length == 0) {
            throw new UnsupportedOperationException("NOT NODE");
        }
        return instances.get((index++) % length);
    }
}
